package com.css.ids.server.service.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.css.apps.base.sys.model.SSys;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;
import com.css.util.ListUtil;
import com.css.util.StringHelper;

public class IdsSysDao {

	public static SSys getSys(String sysId) {
		if (StringHelper.isEmpty(sysId)) {
			return null;
		}
		return QueryCache.get(SSys.class, sysId);
	}
	
	@SuppressWarnings("unchecked")
	public static List<SSys> listSys(Collection<String> sysIds) {
		return QueryCache.idToObj(SSys.class, new ArrayList<>(sysIds));
	}

	@SuppressWarnings("unchecked")
	public static List<SSys> getSysByUserId(String userId) {
        SUser user = QueryCache.get(SUser.class, userId);
        if (user == null) return null;
        return user.getSysList().getList();
    }
	
	public static SSys getSysBySysCode(String sysId) {
        StringBuffer sb = new StringBuffer(" select a.uuid from SSys a where a.openFlag='1' and a.sysId = :sysId ");
        QueryCache qc = new QueryCache(sb.toString()).setParameter("sysId", sysId);
        String uuid = (String) qc.uniqueResult();
        if (uuid == null) return null;
        return QueryCache.get(SSys.class, uuid);
    }
	
	@SuppressWarnings("unchecked")
    public static List<SSys> getAllSys() {
    	StringBuffer sb = new StringBuffer(" select a.uuid from SSys a where a.openFlag='1' ");
		List<String> sysIds = new QueryCache(sb.toString()).list();
        if (ListUtil.isLstEmpty(sysIds)) 
        	return null;
        return  QueryCache.idToObj(SSys.class, sysIds);
    }
}
